package guda.shop.core.tpl;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

public class FileTpl
        implements Tpl {
    private File file;
    private String fileName;

    public FileTpl(File file, String fileName) {
        this.file = file;
        this.fileName = fileName;
    }

    public String getName() {
        String str = this.file.getAbsolutePath().substring(this.fileName.length());
        str = str.replace(File.separatorChar, '/');
        if (!str.startsWith("/"))
            str = "/" + str;
        return str;
    }

    public String getPath() {
        String str = getName();
        return str.substring(0, str.lastIndexOf('/'));
    }

    public String getFilename() {
        return this.file.getName();
    }

    public String getSource() {
        if (this.file.isDirectory())
            return null;
        try {
            return FileUtils.readFileToString(this.file, "UTF-8");
        } catch (IOException localIOException) {
            throw new RuntimeException(localIOException);
        }
    }

    public long getLastModified() {
        return this.file.lastModified();
    }

    public Date getLastModifiedDate() {
        return new Timestamp(getLastModified());
    }

    public long getLength() {
        return this.file.length();
    }

    public int getSize() {
        return (int) (getLength() / 1024L) + 1;
    }

    public boolean isDirectory() {
        return this.file.isDirectory();
    }
}

